'use strict';

const Service = require('egg').Service;
const Mock = require('mockjs');
const dayjs = require('dayjs');
dayjs().format()

class EmailService extends Service {
  async getList() {
    const {
      ctx,
      app
    } = this;
        //     // 模拟数据插入表中
        // const {list} = Mock.mock({
        //     "list|20":[
        //         {
        //             send:"@ctitle(10,20)",
        //             accept:"@ctitle(10,20)",
        //             theme:"@ctitle(4,10)",
        //             create_at:dayjs(Date.now()).format('YYYY-MM-DD')
        //         }
        //     ]            
        // });
        // // 循环list
        // list.forEach((item)=>{
        //     app.mysql.insert('email',{...item});
        // });
        // return 'ok'
    const {
      pageSize,
      pageIndex,
      send,
      accept,
      theme
    } = ctx.query;
    let sql = `select * from email where 1=1 `;
    let countSql = `select count(id) as total from email where 1=1 `
    if (send) {
      sql += `and send="${send}" `
      countSql += `and send="${send}" `
    }
    if (accept) {
      sql += `and accept="${accept}" `
      countSql += `and accept="${accept}" `
    }
    if (theme) {
      sql += `and theme="${theme}" `
      countSql += `and theme="${theme}" `
    }
    if (pageSize && pageIndex) {
      sql += `limit ${(pageIndex-1)*pageSize},${pageSize} `
    }
    const res = await app.mysql.query(sql);
    const [{
      total
    }] = await app.mysql.query(countSql);
    return {
      data: res,
      total,
    }
  }
  async delEmail() {
    const {
      ctx,
      app
    } = this;
    const {
      id
    } = ctx.request.body;
    const res = await app.mysql.delete('email', {
      id
    });
    return res
  }
}

module.exports = EmailService;